import 'package:flutter/material.dart';
import 'package:ali_auth/ali_auth.dart';
import 'home.dart';
import 'dart:io';

void main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  BuildContext mContext;

  @override
  void initState() {
    super.initState();
    // 初始化插件
    if (Platform.isAndroid) {
      AliAuthPlugin.initSdk(
          'J2WOym6iWIFZO2cQPaPmZLLIH+Yfhe+rAZntxJkeZZnIIyf72w+vCs7QWBjyrJgx4A1tvjlrvbbGo9a3JxtWur5EikvBVloPI3m3CqgUpiVkgGnzM1ZLxeATspdDIXYP3n4H/nbgpfAChA+tlqTj/f4vfsxyXsP2x4dS7J5yoA2CPZ6mv6thFHzR6vTJf+JNGEm1BmcnqmDrRkQYoOQDBfYDGV0CXhDUsVD1Dcs56TuP0SgAjj9sSRygIP/JGKz7l8BhIGtRkBZ/E37UrK6eTpYuVwVuMUKoZCFqLSdC2ESBaD3vtAHMFGBx5T9z7OiGBFWHgLbY9HM=');
    } else {
      AliAuthPlugin.initSdk(
          'HBNTQKOk/1d9SyrpwlAaXt6zlyKLDV7QlDlORdcJgiZxImlvUdStBsTdPVmFdT5APteiXlKGfCpVZezZJH7JOBISLed06MGX112Ci8Fj1wQr4lJgEFhlZUfZQDwjj9rFFQCXVNusMspmcvWAOjDxF9Ld1rOtWieYwWKwP4d9A1Ta+MIty28UEnVRTNVRzhoO+IsM/G2XBGpmtUgAFNJD9XbtRzJOOwX5EOGpUvlWtivtCRS612TOW+XGpx2x/+PhkT7vecwzen0Kuh1jlbO0Vgp17bNU5vtK');
    }

    /// 苹果登录专用监听
    /// 如果不需要苹果登录请删除该代码
    AliAuthPlugin.loginListen(
        type: false, onEvent: _onEvent, onError: _onError);
  }

  /// 苹果登录错误处理
  void _onEvent(event) {
    print(
        "------------------------------------------------------------------------------------------$event");
    Navigator.of(mContext).push(new MaterialPageRoute(builder: (_) {
      return Home();
    }));
  }

  /// 苹果登录成功后处理
  void _onError(error) {
    print(
        "==========================================================================================$error");
  }

  @override
  Widget build(BuildContext context) {
    mContext = context;
    return Scaffold(
      appBar: AppBar(
        title: const Text('阿里云一键登录插件'),
      ),
      body: Column(
        children: <Widget>[
          RaisedButton(
            onPressed: () async {
              final result = await AliAuthPlugin.loginDialog;
              print(result);
            },
            child: Text('弹窗登录'),
          ),
          RaisedButton(
            onPressed: () async {
              final result = await AliAuthPlugin.login;
              print(result);
            },
            child: Text('直接登录'),
          ),
          RaisedButton(
            onPressed: () async {
              final checkVerifyEnable = await AliAuthPlugin.checkVerifyEnable;
              print(checkVerifyEnable);
            },
            child: Text('检测环境是否支持'),
          ),
          RaisedButton(
            onPressed: () async {
              await AliAuthPlugin.appleLogin;
            },
            child: Text('apple登录'),
          ),
        ],
      ),
    );
  }
}
